package toDo.app;

import interfaces.IToDoListActivity;

import java.util.ArrayList;
import java.util.List;
import database.DatabaseFacade;
import toDo.app.R;
import toDo.app.controllers.ToDoListActivityClickController;
import toDo.app.controllers.ToDoListActivityResultController;
import toDo.app.controllers.ToDoListActivitySelectedController;
import toDo.app.models.RequestCodes;
import toDo.app.models.ToDoListItem;
import toDo.app.models.User;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

public class ToDoListActivity extends IToDoListActivity {
	private User user;
	private Button createButton, logoutButton;
	/*
	 * private Button all; private Button uncomplete; private Button date;
	 * private Button category;
	 */

	private Spinner categorySpinner, viewSpinner;
	private TextView categoryLabel;
	private List<String> categories, views;
	private List<ToDoListItem> items;
	private ToDoListItem item;
	private ToDoListActivityClickController cController;
	private ToDoListActivityResultController rController;
	private ToDoListActivitySelectedController sController;

	@Override
	public void onCreate(Bundle blah) {
		super.onCreate(blah);
		setContentView(R.layout.todolist);

		user = User.getUserFromIntent(getIntent());
		views = new ArrayList<String>();
		views.add("All");
		views.add("Uncompleted");
		views.add("After Date");
		views.add("Category");
		createButton = (Button) findViewById(R.id.toDoListNewItemLButton);
		// all = (Button) findViewById(R.id.toDoListAllButton);
		// uncomplete = (Button) findViewById(R.id.toDoListUncompletedButton);
		// date = (Button) findViewById(R.id.toDoListAfterDateButton);
		// category = (Button) findViewById(R.id.toDoListCategoryButton);
		logoutButton = (Button) findViewById(R.id.toDoListLogOutLButton);
		viewSpinner = (Spinner) findViewById(R.id.toDoListViewSpinner);
		categorySpinner = (Spinner) findViewById(R.id.toDoListSpinner);
		categoryLabel = (TextView) findViewById(R.id.toDoListCategoyLabel);
		
		cController = new ToDoListActivityClickController(this, CreateToDoActivity.class, EditToDoActivity.class,
				ToDoMapActivity.class);
		rController = new ToDoListActivityResultController(this);
		sController = new ToDoListActivitySelectedController(this);

		// date.setOnClickListener(cController);
		createButton.setOnClickListener(cController);
		logoutButton.setOnClickListener(cController);
		// all.setOnClickListener(cController);
		// uncomplete.setOnClickListener(cController);
		// category.setOnClickListener(cController);
		setList(DatabaseFacade.getList(this, user));

		setCategorySpinner(DatabaseFacade.getListTypes(this, user));
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, views);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		viewSpinner.setAdapter(adapter);
		viewSpinner.setOnItemSelectedListener(sController);
		categorySpinner.setOnItemSelectedListener(sController);
		setCategoryVisibility(false);
		
		
	}

	@Override
	public void onListItemClick(ListView parent, View v, int position, long id) {
		// this manages what happens when a user clicks on a todo item
		//selection.setText(items.get(position).clickString());
		setItem(items.get(position));
		//dialog=new ViewToDoDialog();
		//dialog.show(this, items.get(position));
		Intent intent = new Intent(this,ViewToDoDialog.class);
		ToDoListItem.putItemIntoIntent(intent, items.get(position));
		startActivityForResult(intent, RequestCodes.viewItem);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		rController.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public User getUser() {
		return user;
	}

	@Override
	public List<String> getCategories() {
		return categories;
	}

	@Override
	public void setCategorySpinner(List<String> categories) {
		this.categories = categories;
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		categorySpinner.setAdapter(adapter);
		adapter.notifyDataSetChanged();
	}

	@Override
	public void setList(List<ToDoListItem> items) {
		this.items = items;
		setListAdapter(new ArrayAdapter<ToDoListItem>(this, android.R.layout.simple_list_item_1, items));
	}

	@Override
	public void setCategoryVisibility(boolean visible) {
		if (visible) {
			categorySpinner.setVisibility(View.VISIBLE);
			categoryLabel.setVisibility(View.VISIBLE);
		} else {
			categorySpinner.setVisibility(View.GONE);
			categoryLabel.setVisibility(View.GONE);
		}
	}

	@Override
	public void setItem(ToDoListItem current) {
		this.item = current; 
	}

	@Override
	public ToDoListItem getItem() {
		return item;
	}
	
}